package com.example.yk_lx.friend;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.yk_lx.R;
import com.example.yk_lx.adapter.FriendAdapter;
import com.example.yk_lx.adapter.GroupAdapter;
import com.example.yk_lx.baseMVVM.BaseFragment;
import com.example.yk_lx.databinding.FragmentFriendBinding;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.exceptions.HyphenateException;

import java.util.ArrayList;
import java.util.List;

public class FriendFragment extends BaseFragment<FragmentFriendBinding,FriendViewModel> {

    List<String> friends = new ArrayList<>();
    FriendAdapter friendAdapter;

    List<EMGroup> groups = new ArrayList<>();
    GroupAdapter groupAdapter;

    public FriendFragment() {
        // Required empty public constructor
    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_friend;
    }

    @Override
    protected void initView() {
        friendAdapter = new FriendAdapter(R.layout.item_friend,friends);
        dataBinding.rv.setAdapter(friendAdapter);
        dataBinding.rv.setLayoutManager(new LinearLayoutManager(getContext()));

        viewModel.getFriend();
        viewModel.success.observe(this, new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {
                friends.clear();
                friends.addAll(strings);
                friendAdapter.notifyDataSetChanged();
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
        //点击跳转到详情界面
        friendAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("conversationId",friends.get(position));
                bundle.putInt("chatType",1);
                ARouter.getInstance().build("/app/ChatActivity").withBundle("bundle",bundle).navigation();
            }
        });

        groupAdapter = new GroupAdapter(R.layout.item_group,groups);
        dataBinding.rvGroup.setAdapter(groupAdapter);
        dataBinding.rvGroup.setLayoutManager(new LinearLayoutManager(getContext()));

        viewModel.getGroup();
        viewModel.groupSuccess.observe(this, new Observer<List<EMGroup>>() {
            @Override
            public void onChanged(List<EMGroup> emGroups) {
                groups.clear();
                groups.addAll(emGroups);
                groupAdapter.notifyDataSetChanged();
            }
        });
        viewModel.groupFail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });

        groupAdapter.addChildClickViewIds(R.id.addmember,R.id.jin);
        groupAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId()){
                    case R.id.addmember:
                        ARouter.getInstance().build("/app/AddmemberActivity").withString("groupId",groups.get(position).getGroupId()).navigation();
                        break;
                    case R.id.jin:
                        jin(position);
                        break;
                }
            }
        });
        //点击跳转到详情界面
        groupAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("conversationId",groups.get(position).getGroupId());
                bundle.putInt("chatType",2);
                ARouter.getInstance().build("/app/ChatActivity").withBundle("bundle",bundle).navigation();
            }
        });



    }
    //禁言
    private void jin(int position) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<String> list = new ArrayList<>();
                list.add("lisi");

                try {
                    EMClient.getInstance().groupManager().muteGroupMembers(groups.get(position).getGroupId(),list,1000*60);
                    ToastUtils.showLong("禁言成功");
                } catch (HyphenateException e) {
                    //throw new RuntimeException(e);
                    ToastUtils.showLong("禁言失败");
                }
            }
        }).start();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected Class<FriendViewModel> getViewModel() {
        return FriendViewModel.class;
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.getFriend();
        viewModel.getGroup();
    }
}